﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using MxTol.Models;
using MxTol.UI.Helpers;
using MxTol.Managers;

namespace MxTol.UI.Controllers
{
    /// <summary>
    /// 会计基础判断题控制器
    /// </summary>
    public class BaJudgeManagementController : Controller
    {
        private const string MIME_TYPE = "application/json;charset=utf-8;";

        #region View

        // 添加判断题页面
        public ActionResult AddJudgeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        // 修改判断题页面
        public ActionResult UpdateJudgeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                BaJudge judge = null;
                try
                {
                    judge = BaJudge.GetJudgeByID(new Guid(id));
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLogAsync("修改会计基础判断题信息Action失败", ex);
                }

                if (judge != null)
                {
                    judge.Title = MethodHelper.ToOneLine(judge.Title);
                    return View(judge);
                }
                else
                {
                    return Content("获取判断题信息失败");
                }
            }
        }

        // 查询判断题页面
        public ActionResult QueryJudgeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        #endregion

        #region 功能

        // 以Json分页格式返回所有判断题
        public ActionResult GetBaJudgeJson()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string currentPage = Request["page"];
                string pageSize = Request["rows"];

                json = BaJudgeManager.GetAllJudge(int.Parse(currentPage), int.Parse(pageSize));
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("获取会计基础判断题题库Json格式数据错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 添加判断题
        public ActionResult AddJudge()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string title = Request["title"];
                string key = Request["key"];
                string analysis = Request["analysis"];

                if (Session["account"] != null)
                {
                    BaJudge judge = new BaJudge();

                    judge.Title = title;
                    judge.Answer = byte.Parse(key);
                    judge.Analysis = analysis;
                    judge.AddPerson = AdminManager.GetAdminName(Session["account"].ToString());
                    judge.AddTime = DateTime.Now;

                    judge.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("添加会计基础判断题失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 删除判断题
        public ActionResult DeleteJudge()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                BaJudge judge = BaJudge.GetJudgeByID(new Guid(id));
                if (judge != null)
                {
                    judge.Delete();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("删除会计基础判断题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 修改判断题
        public ActionResult UpdateJudge()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                if (Session["account"] == null)
                {
                    string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                    return Content(script);
                }

                string id = Request["id"];
                BaJudge judge = BaJudge.GetJudgeByID(new Guid(id));
                if (judge != null)
                {
                    string title = Request["title"];
                    string key = Request["key"];
                    string analysis = Request["analysis"];

                    judge.Title = title;
                    judge.Answer = byte.Parse(key);
                    judge.Analysis = analysis;

                    judge.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("修改会计基础判断题失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 查询判断题
        public ActionResult QueryJudge()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string title = Request["title"];
                string key = Request["key"];
                string analysis = Request["analysis"];

                // 全部搜索
                int currentPage = 1;
                string pageSize = Request["rows"];

                // 拼接sql基本查询字段
                string sql_base = "select * from Ba_Judge";
                string sql_where = string.Empty;
                if (!string.IsNullOrEmpty(title))
                {
                    sql_where += string.Format("and title like '%{0}%' ", title);
                }
                if (!string.IsNullOrEmpty(key) && key.Trim() != "2")
                {
                    sql_where += string.Format("and answer_key = '{0}' ", key);
                }
                if (!string.IsNullOrEmpty(analysis))
                {
                    sql_where += string.Format("and analysis like '%{0}%' ", analysis);
                }

                // 有查询条件，没有查询所有
                string sql = sql_base;
                if (sql_where.Length != 0)
                {
                    // 去掉第一个"and"
                    if ((sql_where.TrimStart()).StartsWith("and"))
                    {
                        sql_where = sql_where.Substring(3, sql_where.Length - 3);
                        sql = sql_base + " where " + sql_where;
                    }
                }
                json = BaJudgeManager.GetAllJudge(currentPage, int.Parse(pageSize), sql);
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("查询会计基础判断题错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        #endregion
    }
}
